package com.c2c.controller.mall;

import com.c2c.converter.DataConverterUtil;
import com.c2c.mapper.GoodsClassifyMapper;
import com.c2c.mapper.GoodsMapper;
import com.c2c.po.Comment;
import com.c2c.po.Goods;
import com.c2c.po.GoodsClassify;
import com.c2c.po.User;
import com.c2c.service.AccountService;
import com.c2c.service.CommentService;
import com.c2c.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/Product")
public class MallIndexController {

    @Resource
    GoodsService goodsService;

    @Resource
    CommentService commentService;

    @Resource
    AccountService accountService;


    @RequestMapping("/Secher")
    public String Findsomething(HttpServletRequest request, String string) {
        List<Goods> goodsList = goodsService.findBySomething("String", 0);
        request.setAttribute("goodsList", goodsList);
        return "products";
    }

    @RequestMapping("/Category")
    public String CategoryList(HttpServletRequest request) {
        List<GoodsClassify> goodsClassifyList = goodsService.findAll();
        request.setAttribute("goodsClassifyList", goodsClassifyList);
        List<Goods> goodsList = goodsService.findByClassifyId(1);
        request.setAttribute("goodsList", goodsList);
        return "products";
    }

    @RequestMapping("/Show")
    public String ShowProducts(HttpServletRequest request, Integer goodsCategoryId) {
        List<GoodsClassify> goodsClassifyList = goodsService.findAll();
        request.setAttribute("goodsClassifyList", goodsClassifyList);
        List<Goods> goodsList = goodsService.findByClassifyId(goodsCategoryId);
        request.setAttribute("goodsList", goodsList);
        return "products";
    }

    @RequestMapping("/commodity")
    public ModelAndView Shop(Integer goodsId, ModelAndView modelAndView) {
        //查找到商品内容
        Goods commodity = goodsService.findById(goodsId);
        //商品对应评论
        List<Comment> byGoodsId = commentService.findByGoodsId(goodsId);
        //根据评论内用户ID查询
        for (int i = 0; i < byGoodsId.size(); i++) {
            User byUserAccount = accountService.findByUserAccount(byGoodsId.get(i).getUserAccount());
            byGoodsId.get(i).setUser(byUserAccount);
        }
        modelAndView.setViewName("detail");
        modelAndView.addObject("commodity", commodity);
        modelAndView.addObject("byGoodsId", byGoodsId);
        return modelAndView;
    }
}
